Skip to content

Fix -0.0 serialization for implicit-presence float and double fields#1443

Merged
timostamm merged 3 commits into
mainfrom
tstamm/Fix--0.0-serialization-for-implicit-presence-float-and-double-fields
Jun 22, 2026
Merged

Fix -0.0 serialization for implicit-presence float and double fields#1443
timostamm merged 3 commits into
mainfrom
tstamm/Fix--0.0-serialization-for-implicit-presence-float-and-double-fields

Conversation

@timostamm

Copy link
Copy Markdown
Member

With implicit presence (the proto3 default for singular scalar fields), the value decides whether a field is serialized:
false, "", and 0 are considered not set, and are omitted from the wire.

But there is a special case for float and double fields. The proto3 spec on Default Field Values:

If a float or double value is set to +0 it will not be serialized, but -0 is considered distinct and will be serialized.

We missed this detail. isScalarZeroValue from @bufbuild/protobuf/reflect treats -0.0 as a zero value, so a singular float or double field set to -0.0 is omitted during serialization, losing its sign in a round-trip.

This fixes the behavior to align with the spec. Note that this is a behavior change: -0.0 in a singular implicit-presence field is now written to the wire instead of omitted. The function equals inherits the fixed behavior for field presence, but float and double values are still compared with IEEE semantics: NaN does not equal NaN, and -0 equals 0.

@timostamm timostamm requested a review from ajeetdsouza June 19, 2026 15:33
@vercel

vercel Bot commented Jun 19, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
protobuf-es Ignored Ignored Preview Jun 22, 2026 1:19pm

Request Review

…lization-for-implicit-presence-float-and-double-fields
…lization-for-implicit-presence-float-and-double-fields
@timostamm timostamm merged commit 579f79b into main Jun 22, 2026
27 checks passed
@timostamm timostamm deleted the tstamm/Fix--0.0-serialization-for-implicit-presence-float-and-double-fields branch June 22, 2026 13:21
@timostamm timostamm mentioned this pull request Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants